local ExternalFun = appdf.req(appdf.EXTERNAL_SRC .. "ExternalFun")
local MultiPlatform = appdf.req(appdf.EXTERNAL_SRC .. "MultiPlatform")
local ClipText = appdf.req(appdf.EXTERNAL_SRC .. "ClipText")
local HeadSprite = appdf.req(appdf.EXTERNAL_SRC .. "HeadSprite")
local loginCMD = appdf.req(appdf.HEADER_SRC .. "CMD_LogonServer")
local utils = appdf.req(appdf.EXTERNAL_SRC .. "Utils")

--个人信息界面
local BankBankLayer = class("BankBankLayer", function(scene)
	local banklayer = display.newLayer(cc.c4b(0, 0, 0, 180))
	return banklayer
end)

local bGender = false
local Shop = appdf.req(appdf.CLIENT_SRC .. "plaza.views.layer.plaza.ShopLayer")
local labColor = cc.c3b(204, 172, 139)
local tbBankName = {}
function BankBankLayer:ctor(scene)
	ExternalFun.registerNodeEvent(self)
	self._scene = scene
	local this = self
	tbBankName["中国工商银行"] = "ICBC"
	tbBankName["中国农业银行"] = "ABC"
	tbBankName["中国银行"] = "BOC"
	tbBankName["中国建设银行 "] = "CCB"
	tbBankName["交通银行"] = "COMM"
	tbBankName["招商银行"] = "CMB"
	tbBankName["中信银行"] = "CITIC"
	tbBankName["中国民生银行"] = "CMBC"
	tbBankName["华夏银行"] = "HXB"
	tbBankName["中国邮政储蓄银行"] = "PSBC"
	tbBankName["浦发银行"] = "SPDB"
	tbBankName["兴业银行"] = "CIB"
	tbBankName["中国光大银行"] = "CEB"
	tbBankName["平安银行"] = "PAB"
	
	self:loadCSB()
	self:init()
	self:setContentSize(yl.WIDTH, yl.HEIGHT)

	local events = {"SETBANKID",}
	self.tb_listen =  appdf.initListener(events,handler(self,self.customEventHandler),self)
	
	--区域外取消显示
	local  onEndTouch = function(pTouch,pEvent)
		local point = pTouch:getLocation()  
		local rect = self.banknameList:getBoundingBox()  
		if cc.rectContainsPoint(rect,point) == false then
			self.banknameList:setVisible(false)
		end
	end
	local onTouchBegan = function(pTouch,pEvent)	
		return true
	end
	local listener1 = cc.EventListenerTouchOneByOne:create() 
	self.listener1 = listener1
	listener1:setSwallowTouches(false)
		listener1:registerScriptHandler(onEndTouch,cc.Handler.EVENT_TOUCH_ENDED )
		 listener1:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
	 self.eventDispatcher = self.banknameList:getEventDispatcher() --事件派发器  
    
	self.eventDispatcher:addEventListenerWithSceneGraphPriority(listener1,self.banknameList) --分发监听事  
end
function BankBankLayer:onExit()
    self.eventDispatcher:removeEventListener(self.listener1)  
	appdf.removeListener(self.tb_listen)
end
function BankBankLayer:customEventHandler(event)
	if event.name == "SETBANKID" then
		if event.data.result == 1 then
			
			if self.bandingInfo.type == 1 then	--ali
				showToast(self._scene, "支付宝账号绑定成功!", 2)
				GlobalUserItem.szAliPayAcccount = self.bandingInfo.bindid
				GlobalUserItem.szRealyName = self.bandingInfo.bindname
			
			elseif self.bandingInfo.type == 2 then	--bank
				showToast(self._scene, "银行卡账号绑定成功!", 2)
				GlobalUserItem.bankid = self.bandingInfo.bindid
				GlobalUserItem.bank_username = self.bandingInfo.bindname
			end
			self._scene:reSetBindingInfo(self.bandingInfo)
			self:removeFromParent()
		end

	end
end

function BankBankLayer:loadCSB()
	local _, root = ExternalFun.loadRootCSB("userinfo/bindBankLayer.csb", self)
	self.bg = root:getChildByName("bg")
	ExternalFun.FixScaleUI(self.bg)
	local bg = self.bg
	ExternalFun.playOpenLayerAnimation( self.bg )
	self.btn_close = bg:getChildByName("btn_close")
	self.btn_confirm = bg:getChildByName("btn_confirm")
	self.tf_banknu = bg:getChildByName("tf_banknu")
	self.tf_cardname = bg:getChildByName("tf_cardname")
	self.selectbankBtn = bg:getChildByName("selectbankBtn")
	self.tf_bankarea = bg:getChildByName("tf_bankarea")
	self.txt_tips = bg:getChildByName("txt_tips")
	self.banknameList = bg:getChildByName("banknameList")
	self.banknameList:setLocalZOrder(100)
	self.banknameList:setVisible(false)
	self.mySecletbankname_text = self.selectbankBtn:getChildByName("Text_21")
	local btnCallback = function(ref,type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			ExternalFun.playClickEffect()
			self:onButtonClicked(ref:getTag(),ref)
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then
			ref:setScale(1)
		end
	end
	self.selectbankBtn:addTouchEventListener(btnCallback)
	self.btn_close:addTouchEventListener(btnCallback)
	self.btn_confirm:addTouchEventListener(btnCallback)
	local allBankNameItems = self.banknameList:getItems()
	for k,v in pairs(allBankNameItems) do
		v:addTouchEventListener(function(ref,type)
			if type == ccui.TouchEventType.ended then
				ExternalFun.playClickEffect()
				
				self.mySecletbankname_text:setString(ref:getChildByName("bankname"):getString())
				self.banknameList:setVisible(false)
				self.mySecletbankname_text:setTextColor(cc.c3b(255,255,255))
			end
		end)
	end
end

function BankBankLayer:init()
	self.bandingInfo = {}
	local size = self.tf_banknu:getContentSize()
	local posx,posy = self.tf_banknu:getPosition()
	self.tf_banknu:removeFromParent()
	self.tf_banknu = nil

	self.tf_banknu =  ccui.EditBox:create(size,ccui.Scale9Sprite:create(""))
	:setAnchorPoint(cc.p(0.5,0.5))
	:setPosition(cc.p(posx,posy))
	:setFontSize(28)
	:setPlaceholderFontSize(28)
	:setInputMode(cc.EDITBOX_INPUT_MODE_NUMERIC)
	:setMaxLength(20)
	:setPlaceHolder("请输入银行卡卡号")

	size = self.tf_cardname:getContentSize()
	posx,posy = self.tf_cardname:getPosition()
	self.tf_cardname:removeFromParent()
	self.tf_cardname =  ccui.EditBox:create(size,ccui.Scale9Sprite:create(""))
	:setAnchorPoint(cc.p(0.5,0.5))
	:setPosition(cc.p(posx,posy))
	:setFontSize(28)
	:setPlaceholderFontSize(28)
	:setMaxLength(30)
	:setPlaceHolder("输入银行卡收款人姓名")
	
	size = self.tf_bankarea:getContentSize()
	posx,posy = self.tf_bankarea:getPosition()
	self.tf_bankarea:removeFromParent()
	self.tf_bankarea =  ccui.EditBox:create(size,ccui.Scale9Sprite:create(""))
	:setAnchorPoint(cc.p(0.5,0.5))
	:setPosition(cc.p(posx,posy))
	:setFontSize(28)
	:setPlaceholderFontSize(28)
	:setMaxLength(28)
	:setPlaceHolder("输入开户行支行")

	self.bg:addChild(self.tf_banknu)
	self.bg:addChild(self.tf_cardname)
	self.bg:addChild(self.tf_bankarea)
end

-- type: 1 - 支付宝 2 - 银行卡
function BankBankLayer:onConfirm()
	msg = {}
	msg.type = 2
	local bknus = string.rtrim( string.ltrim( self.tf_banknu:getText() ))
	local bkname = tbBankName[self.mySecletbankname_text:getString()] 
	local tf_cardname = string.rtrim( string.ltrim(  self.tf_cardname:getText() ))
	local tf_bankarea = string.rtrim( string.ltrim(  self.tf_bankarea:getText() ))
	if bknus == "" or bkname == "" or tf_cardname == "" or tf_bankarea == "" then
		showToast(self, "请输入正确的绑定信息", 2, cc.c3b(255, 255, 0))
		return 
	end
	msg.bindid = bknus
	msg.bindname = tf_cardname
	msg.bankname = bkname
	msg.bankaddr = tf_bankarea
	msg.banknamechina = self.mySecletbankname_text:getString()
	self.bandingInfo = msg
	local calback = function ()
		appdf.clientSocket:sendBindMoneyInfo(msg)
	end
	self:showBindTipsLayer(msg,calback)

end
function BankBankLayer:showBindTipsLayer(info,cb)
	local csbnode = ExternalFun.loadCSB("userinfo/bindTipsLayer.csb", self)
	self.bindtiplayer = csbnode
	local banklayer = csbnode:getChildByName("bank_Panel")
	local ali_Panel = csbnode:getChildByName("ali_Panel")
	ali_Panel:setVisible(false)
	ExternalFun.playOpenLayerAnimation( banklayer )
	banklayer:getChildByName("bankaccount"):setString("银行卡账号："..info.bindid)
	banklayer:getChildByName("myname"):setString("收款人姓名："..info.bindname)
	banklayer:getChildByName("bankname"):setString("开户银行："..info.banknamechina)
	banklayer:getChildByName("subbankname"):setString("开户支行："..info.bankaddr)
	banklayer:getChildByName("ok_btn"):addTouchEventListener(function(ref,type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			ExternalFun.playClickEffect()
			if cb then
				cb()
			end
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then
			ref:setScale(1)
		end
	end)
	banklayer:getChildByName("closebtn"):addTouchEventListener(function(ref,type)
		if type == ccui.TouchEventType.ended then
			ref:setScale(1)
			ExternalFun.playClickEffect()
			csbnode:removeFromParent()
		elseif type == ccui.TouchEventType.began then
			ref:setScale(yl.btscale)
			return true
		elseif type == ccui.TouchEventType.canceled then
			ref:setScale(1)
		end
	end)
end
function BankBankLayer:onButtonClicked(tag, ref)
	if ref == self.btn_close then
		self:removeFromParent()
	elseif ref == self.btn_confirm then
		self:onConfirm()
	elseif ref == self.selectbankBtn then
		if self.banknameList:isVisible() then
			self.banknameList:setVisible(false)
		else
			self.banknameList:setVisible(true)
			self.banknameList:setScale(1,0.6)
			self.banknameList:runAction(cc.ScaleTo:create(0.2,1))
			
	
			
		end

	end
end

return BankBankLayer